package C

import "reflect"

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/7/5 19:43
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type GT func(interface{}, int, int) bool
type Change func(interface{}, int, int)
type Compare func(interface{}, interface{}) bool

var ArrayGT GT = func(arrayV interface{}, index1 int, index2 int) bool {
	value := reflect.ValueOf(arrayV)
	if value.Kind() != reflect.Array && value.Kind() != reflect.Slice {
		return false
	}
	if value.Len() <= index1 || value.Len() <= index2 {
		return false
	}
	array, ok := arrayV.([]int)
	if !ok {
		array1, ok1 := arrayV.([]interface{})
		if !ok1 {
			return false
		}
		return array1[index1].(int) > array1[index2].(int)
	}
	return array[index1] > array[index2]
}

func ArrayChange() Change {
	return func(arrayV interface{}, index1 int, index2 int) {
		value := reflect.ValueOf(arrayV)
		if value.Kind() != reflect.Array && value.Kind() != reflect.Slice {
			return
		}
		if value.Len() <= index1 || value.Len() <= index2 {
			return
		}
		array, ok := arrayV.([]int)
		if !ok {
			array1, ok1 := arrayV.([]interface{})
			if !ok1 {
				return
			}
			array1[index1], array1[index2] = array1[index2], array1[index1]
			return
		}
		array[index1], array[index2] = array[index2], array[index1]
	}
}

func ArrayCompare() Compare {
	return func(a, b interface{}) bool {
		return a.(int) > b.(int)
	}
}
